package org.bsc.mgwt.client.widget.event;

import org.bsc.mgwt.client.widget.celllist.CellEditHandler;

import com.google.gwt.touch.client.Point;
import com.google.web.bindery.event.shared.Event;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.HandlerRegistration;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSelectedEvent;

/**
 * 
 * @author bsorrentino
 *
 */
public class CellEditEvent extends Event<CellEditHandler> {

	public enum Action {
		ADD, DELETE
	};

	private final Action action;
	private final CellSelectedEvent sourceEvent;

	public CellEditEvent(Action action, CellSelectedEvent sourceEvent) {
		super();
		this.action = action;
		this.sourceEvent = sourceEvent;
	}

	public static final Type<CellEditHandler> TYPE = new Type<CellEditHandler>();

	public static void fire(EventBus eventBus, Action action, CellSelectedEvent sourceEvent) {
		eventBus.fireEvent(new CellEditEvent(action, sourceEvent));
	}

	public static HandlerRegistration register(EventBus eventBus, CellEditHandler handler) {
		return eventBus.addHandler(TYPE, handler);
	}

	@Override
	public com.google.web.bindery.event.shared.Event.Type<CellEditHandler> getAssociatedType() {
		return TYPE;
	}

	@Override
	protected void dispatch(CellEditHandler handler) {
		handler.onCellEdit(this);
	}

	public Action getAction() {
		return action;
	}

	public int getIndex() {
		return sourceEvent.getIndex();
	}

	public Point getTouchPoint() {
		return sourceEvent.getTouchPoint();
	}
}
